<template>
  <div class="goods-cate">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 添加按钮 -->
      <el-row>
        <el-col>
          <el-button
            type="primary"
            @click="showAddDialog">
            添加分类
          </el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <!-- row-key="id" -->
      <el-table
        :data="goodsList"
        border
        row-key="cat_id"
        lazy
        :load="goodsLazyload"
        ref="cateTable"
        :tree-props="{children: 'children', hasChildren: 'children'}">
        <el-table-column prop="cat_name" label="分类商品"></el-table-column>
        <el-table-column
          label="是否有效"
          class="is"
          v-slot="data">
          <i v-if="data.row.cat_deleted" class="el-icon-error" style="color: red"></i>
          <i v-else class="el-icon-success" style="color: lightgreen"></i>
        </el-table-column>
        <el-table-column
          prop="cat_level"
          label="排序"
          v-slot="data">
          <el-tag size="mini" v-if="data.row.cat_level === 0">一级</el-tag>
          <el-tag size="mini" type="success" v-else-if="data.row.cat_level === 1">二级</el-tag>
          <el-tag size="mini" type="warning" v-else-if="data.row.cat_level === 2">三级</el-tag>
        </el-table-column>
        <el-table-column
          label="操作"
          v-slot="data"
          min-width="200px">
          <!-- 编辑按钮 -->
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit">
            编辑
          </el-button>
          <!-- 删除按钮 -->
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete" @click="void(data)">
            删除
          </el-button>
        </el-table-column>
      </el-table>
      <!-- 新增分类dialog -->
      <el-dialog
        title="提示"
        :visible.sync="addCateDialogVisible"
        @close="closeAddCateDialog"
        width="50%">
        <el-form
          label-width="80px">
          <el-form-item label="分类名称:">
            <el-input style="width: 220px" v-model="addCateForm.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父类分类:">
            <el-cascader
              v-model="addCateForm.cat_pid"
              :options="cateList"
              :props="cateCascaderProps"
              @change="handleCateChange"></el-cascader>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="addCateDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="confirmAddCate">确 定</el-button>
          </span>
        </template>
      </el-dialog>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[2, 5, 10, 50]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryInfo.total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'GoodsCate',
  data () {
    return {
      // 查询参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10,
        total: 0
      },
      // 存放商品列表
      goodsList: [],
      // 存放分类目录
      cateList: [],
      /* 添加分类对话框show */
      addCateDialogVisible: false,
      // 添加分类表单
      addCateForm: {
        cat_pid: [],
        cat_name: '',
        cat_level: 0,
        detail_pid: []
      },
      // 级联表属性
      cateCascaderProps: {
        expandTrigger: 'hover',
        checkStrictly: true,
        label: 'cat_name',
        value: 'cat_id',
        children: 'children'
      }
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    // 1. 获取商品数据
    async getGoodsList () {
      const { data: res } = await this.categoryHttpRequest(3)
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败')
      }
      // 获取成功!
      this.queryInfo.total = res.data.total
      this.goodsList = res.data.result
      // console.log(this.goodsList)
    },
    // 获取分类目录
    async getCateList () {
      const { data: res } = await this.categoryHttpRequest(2)
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取分类目录失败')
      }
      // 获取成功
      this.cateList = res.data.result
      console.log(this.cateList)
    },
    // 3. 获取分类的数据请求
    categoryHttpRequest (type = '3') {
      return this.$http.get('/categories', {
        params: {
          type,
          query: this.queryInfo.query,
          pagenum: this.queryInfo.pagenum,
          pagesize: this.queryInfo.pagesize
        }
      })
    },
    // 4. 懒加载table的tree分支
    goodsLazyload (tree, treeNode, resolve) {
      setTimeout(() => {
        resolve(tree.children)
      }, 300)
    },
    // 5. show添加分类对话框
    showAddDialog () {
      this.addCateDialogVisible = true
      this.getCateList()
    },
    // 6. 点击级联表的分类
    handleCateChange (pidArr) {
      // 根据pid数组的length赋予level
      this.addCateForm.cat_level = pidArr.length
      this.addCateForm.detail_pid = this.addCateForm.cat_pid
      this.addCateForm.cat_pid = this.addCateForm.cat_pid[this.addCateForm.cat_pid.length - 1]
      console.log(this.addCateForm)
    },
    // 7. 确认添加分类
    async confirmAddCate () {
      const { data: res } = await this.$http.post('/categories', {
        cat_pid: this.addCateForm.cat_pid,
        cat_name: this.addCateForm.cat_name,
        cat_level: this.addCateForm.cat_level
      })
      console.log(res)
      if (res.meta.status !== 201) {
        return this.$message.error('添加分类失败')
      }
      // 成功提示
      this.$message.success('添加分类成功')
      // 获取新的数据
      this.getGoodsList()
      // 刷新分类列表
      // let allLevel = this.addCateForm.cat_level
      // let level = 0
      // let list = this.goodsList
      // while (allLevel) {
      //   list = list.filter(value => {
      //     return value.cat_id === this.addCateForm.detail_pid[level]
      //   })[0]
      //   if (list.children) { list = list.children }
      //   level++
      //   allLevel--
      //   console.log(list)
      // }
      // this.$set(list, list.length - 1, list[length - 1])
      // 关闭dialog
      this.addCateDialogVisible = false
    },
    // 8. 关闭添加分类对话框
    closeAddCateDialog () {
      this.addCateForm.cat_pid = []
      this.addCateForm.cat_name = ''
      this.addCateForm.cat_level = 0
    },
    // 点击页码跳转
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    }
  }
}
</script>

<style scoped lang="less">
.goods-cate {
  .el-card {
    margin-top: 15px;
  }
  .el-breadcrumb {
    font-size: 12px;
  }
  .el-table {
    margin-top: 15px;
  }
}
</style>
